package com.edgclub.edg.fragment;

import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.edgclub.edg.R;
import com.edgclub.edg.adapter.UserImageAdapter;
import com.edgclub.edg.api.ResultCode;
import com.edgclub.edg.api.UserApi;
import com.edgclub.edg.api.param.UserParam;
import com.edgclub.edg.http.VolleyListener;
import com.edgclub.edg.model.PostsAlbum;
import com.edgclub.edg.model.User;
import com.edgclub.edg.model.result.UserPostsImage;
import com.edgclub.edg.utils.util.JSONHelper;
import com.edgclub.edg.utils.util.JsonUtil;
import com.edgclub.edg.utils.util.UserPrefUtils;
import com.jcodecraeer.xrecyclerview.XRecyclerView;

import java.util.ArrayList;
import java.util.List;

public class OtherUserImageFragment extends GzBaseFragment implements VolleyListener{
    private static final String ARG_PARAM1 = "param1";

    private XRecyclerView recyclerView;

    private int currPage = 1;
    private int pageSize = 21;
    private UserApi userApi;
    private UserPrefUtils userPrefUtils;
    private User toUser;
    private List<PostsAlbum> imageList=new ArrayList<>();
    private boolean load = false;
    private UserImageAdapter adapter;
    private TextView text_empty;
    private ImageView imageView_empty;
    private LinearLayout layout_empty;

    public static OtherUserImageFragment newInstance(User param1) {
        OtherUserImageFragment fragment = new OtherUserImageFragment();
        Bundle args = new Bundle();
        args.putSerializable(ARG_PARAM1, param1);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            toUser = (User) getArguments().getSerializable(ARG_PARAM1);
        }
        if (null==toUser)toUser=new User();
        userApi=new UserApi(getActivity());
        userPrefUtils=new UserPrefUtils(getActivity());
        adapter=new UserImageAdapter(getActivity());
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fagment_user_image, container, false);
    }
    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        layout_empty= (LinearLayout) view.findViewById(R.id.layout_empty);;
        text_empty= (TextView) view.findViewById(R.id.text_empty);;
        imageView_empty = (ImageView) view.findViewById(R.id.imageView_empty);
        imageView_empty.setVisibility(View.VISIBLE);
        imageView_empty.setImageResource(R.drawable.nomess);
        text_empty.setText("还没有相关内容哦");
        recyclerView = (XRecyclerView) view.findViewById(R.id.recyclerview);
        GridLayoutManager layoutManager = new GridLayoutManager(getActivity(),3);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(layoutManager);


        recyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                currPage = 1;
                getUserImage(currPage);
            }

            @Override
            public void onLoadMore() {
                getUserImage(currPage);
            }
        });
        getUserImage(currPage);
        adapter.setDatas(imageList);
        recyclerView.setAdapter(adapter);
    }

    private void getUserImage(int page){
        if(load)
            return;
        UserParam userParam=new UserParam();
        userParam.setUserId(userPrefUtils.getUser().getId());
        userParam.setToUserId(toUser.getId());
        userParam.setPage(page+"");
        userParam.setPageSize(pageSize+"");
        userApi.getOtherImage(userParam,this);
    }


    @Override
    public void onStart(String tag) {
        load =true;
    }

    @Override
    public void onSuccess(String response, String tag) {
        load =false;
        recyclerView.refreshComplete();
        recyclerView.loadMoreComplete();
        if (JSONHelper.isSuccess(response)) {
            UserPostsImage userImage = JsonUtil.getMode(response,
                    UserPostsImage.class);
            if(null!=userImage&&null!=userImage.getData()&&userImage.getData().getPostsAlbums().size()>0){
                if(currPage==1){
                    imageList.clear();
                }
                currPage++;
                imageList.addAll(userImage.getData().getPostsAlbums());
                adapter.notifyDataSetChanged();
            }else{
                recyclerView.noMoreLoading();
            }
        }

        if (!"".equals(imageList)&&imageList.size()>0){
            layout_empty.setVisibility(View.GONE);
            recyclerView.setPullRefreshEnabled(true);
        }
        else {
            layout_empty.setVisibility(View.VISIBLE);
            recyclerView.setPullRefreshEnabled(false);
        }
    }

    @Override
    public void onFaile(VolleyError volleyError, String tag) {
        load =false;
        recyclerView.refreshComplete();
        recyclerView.loadMoreComplete();
        ResultCode.toastVolleyError(getActivity(), volleyError);
    }
}
